
<template>
    <theme-box title="投票活动添加">
        <form class="form-inline" @submit.prevent v-el:form>
            <div class="box-body">
                <form-input title="活动名称" :required="true" name="actName"></form-input>
                <form-input title="活动名次每级奖励" :required="true" name="actAward"></form-input>
                <form-input title=" 活动奖励名次" :required="true" name="awardNum"></form-input>
                <form-input title="活动第一名奖励" :required="true" name="awardFir"></form-input>
                <form-date-time  title="开始时间" :required="true" name="beginDate"></form-date-time>
                <form-date-time  title="结束时间" :required="true"  name="endDate"></form-date-time>
            </div>
            <!-- /.box-body -->
            <div class="box-footer">
                <v-button type="reset" @click="reset">重置</v-button>
                <v-button type="submit" theme="primary" class="pull-right">确认</v-button>
            </div>
            <!-- /.box-footer -->
        </form>
    </theme-box>
</template>

<script>
    import 'plugins/jquery-validation';
    import {
        dateTime,
        createDataTable
    } from 'ui/collections/general.js'
    import {dateFormat} from "lib/method.js";
    var serialize = require('form-serialize');


    export default {
        data: () => ({

        }),
        methods: {
            reset() {
                $("label.error").remove();
                this.$els.form.reset()
            }
        },
        ready() {
            dateTime({
                format: "yyyy-mm-dd",
                minView: "2",
            });
            var self = this;
            $(this.$els.form).validate({
                submitHandler: async function() {
                    var _data = serialize(this.currentForm, {
                        hash: true,
                        empty: true
                    });
                    //对日期进行数据处理
                    _data.beginDate = _data.beginDate.replace(/-/g, "");
                    _data.endDate = _data.endDate.replace(/-/g, "");

                    var result = await self.$post({
                        url: "/api/v4"+ projectName_Url +"/plat/voteAct/add",
                        data: _data
                    })
                    //奖励规则:活动第一名奖励 - 活动名次每级奖励 * (活动奖励名次-1) >= 1;
//                    if(parseInt(_data.awardFir) - parseInt(_data.actAward)* (parseInt(_data.awardNum)-1) >= 1){
//
//                    }else{
//                        Midway.alert({title: "操作提示", content:'请合理设置奖励规则'});
//                        return false;
//                    }
                    let data = {
                        title: "操作成功",
                        content: "是否进入投票管理查看?"
                    }
                    Midway.confirm(data).then(function () {
                        self.$route.router.go('/votingActivity/list');
                    })
                    self.reset()
                },

                errorPlacement: function(error, element) {
                    if (element.attr("name") == "file") {
                        error.insertAfter(".file-item");
                    } else {
                        error.insertAfter(element)
                    }
                },
                rules: {
                    actName:{
                        required: true,
                        maxlength:15
                    },
                    actAward: {
                        required: true,
                        int:true,
                        max:99,
                        min:1
                    },
                    awardFir:{
                        required: true,
                        int:true,
                        max:99,
                        min:1
                    },
                    awardNum:{
                        required: true,
                        int:true,
                        max:99,
                        min:1
                    },
                    beginDate:{
                        required: true,
                    },
                    endDate:{
                        required: true,
                    }
                },
            });
        }
    }
</script>
